package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Value("${fastdfs-server}")
    private String fastdfsServer;

    /**
     * 素材分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo findPage(WmMaterialPageRequestDto dto) {
        //1. 构建分页对象
        IPage<WmMaterial> pageInfo = new Page<>(dto.getPage(),dto.getSize());
        //2. 构建条件对象
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        // 是否为收藏
        lqw.eq(dto.getIsCollection() ==1,WmMaterial::getIsCollection,dto.getIsCollection());
        // 是否为当前用户的素材
        lqw.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());
        //3. 执行查询
        page(pageInfo,lqw);
        // 给素材补充全路径
        //   获取结果集
        List<WmMaterial> list = pageInfo.getRecords();
        list.forEach(WmMaterial->WmMaterial.setUrl(fastdfsServer + WmMaterial.getUrl()));
        //4. 封装结果返回
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),pageInfo.getTotal(),pageInfo.getRecords());
    }
}
